package com.fitz.controller;

import com.dangdang.ddframe.rdb.sharding.keygen.KeyGenerator;
import com.fitz.entity.Goods;
import com.fitz.repository.GoodsRepository;
import com.fitz.task.BatchExecutorData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
/**
 * 描    述：
 * 创 建 者：gousili
 * 修 改 者：
 * 创建时间：2020-01-06 16:41
 */
@RestController
public class GoodsController {

    @Autowired
    private KeyGenerator keyGenerator;

    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private BatchExecutorData executorData;

    @GetMapping("/save")
    public String save(){
        for(int i= 1 ; i <= 40 ; i ++){
            Goods goods = new Goods();
            goods.setGoodsId((long) i);
            goods.setGoodsName( "shangpin" + i);
            goods.setGoodsType((long) (i+1));
            goodsRepository.save(goods);
        }
        return "success";
    }

    @GetMapping("/select")
    public String select(){
        return goodsRepository.findAll().toString();
    }

    @GetMapping("/delete")
    public void delete(){
        goodsRepository.deleteAll();
    }

    @GetMapping("/query1")
    public Object query1(){
        return goodsRepository.findAllByGoodsIdBetween(10L, 30L);
    }

    @GetMapping("/query2")
    public Object query2(){
        List<Long> goodsIds = new ArrayList<>();
        goodsIds.add(10L);
        goodsIds.add(15L);
        goodsIds.add(20L);
        goodsIds.add(25L);
        return goodsRepository.findAllByGoodsIdIn(goodsIds);
    }

    @GetMapping("/batch")
    public void batch(){
       List<String> dataList = new ArrayList<>();
       for (int i = 0; i < 2; i++){
           dataList.add("this is test message " + i);
       }
        executorData.addList(dataList);
    }
}

